package es.buffygilmore.collectionapp;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import es.buffygilmore.collectionapp.dialog.ConfirmacionEliminarDialogFragment;
import es.buffygilmore.collectionapp.vo.Coleccion;

public class ColeccionEdit extends FragmentActivity implements
ConfirmacionEliminarDialogFragment.NoticeDialogListener {

	private EditText mTitleText;
	private EditText mBodyText;
	private Long mRowId;
	private CollectionAppDbAdapter mDbHelper;
	private ImageView confirmButton;
	private ImageView eliminarButton;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mDbHelper = new CollectionAppDbAdapter(this);

		this.mDbHelper.open();

		this.setContentView(R.layout.edicion_coleccion);
		this.setTitle(R.string.edit_note);

		this.mTitleText = (EditText) this.findViewById(R.id.titulo_coleccion);
		this.mBodyText = (EditText) this.findViewById(R.id.descripcion_coleccion);

		this.confirmButton = (ImageView) this.findViewById(R.id.guardar_coleccion);
		this.eliminarButton = (ImageView) this.findViewById(R.id.botonDeleteColeccion);
		this.eliminarButton.setEnabled(false);
		this.eliminarButton.setColorFilter(0x55BD8ED7, PorterDuff.Mode.MULTIPLY);

		this.mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
				.getSerializable(CollectionAppDbAdapter.KEY_COLECCION_ROWID);
		if (this.mRowId == null) {
			final Bundle extras = this.getIntent().getExtras();
			this.mRowId = extras != null ? extras
					.getLong(CollectionAppDbAdapter.KEY_COLECCION_ROWID) : null;
		}

		this.populateFields();

		this.confirmButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View view) {
				ColeccionEdit.this.confirmButton.setColorFilter(0xEEBD8ED7,
						PorterDuff.Mode.MULTIPLY);
				ColeccionEdit.this.saveState();
				ColeccionEdit.this.setResult(RESULT_OK);
				ColeccionEdit.this.finish();
			}

		});
		this.eliminarButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View view) {
				ColeccionEdit.this.eliminarButton.setColorFilter(0xEEBD8ED7,
						PorterDuff.Mode.MULTIPLY);

				ColeccionEdit.this.showConfirmDialog();
				ColeccionEdit.this.eliminarButton.clearColorFilter();

			}

		});
	}

	private void populateFields() {
		if (this.mRowId != null) {
			final Cursor coleccion = this.mDbHelper.obtenerColeccion(this.mRowId);
			this.startManagingCursor(coleccion);
			this.mTitleText.setText(coleccion.getString(coleccion
					.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_COLECCION_TITULO)));
			this.mBodyText.setText(coleccion.getString(coleccion
					.getColumnIndexOrThrow(CollectionAppDbAdapter.KEY_COLECCION_DESCRIPCION)));
			this.eliminarButton.setEnabled(true);
			this.eliminarButton.clearColorFilter();
		}
	}

	@Override
	protected void onSaveInstanceState(final Bundle outState) {
		super.onSaveInstanceState(outState);
		// this.saveState();
		outState.putSerializable(CollectionAppDbAdapter.KEY_COLECCION_ROWID, this.mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// this.saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.populateFields();
	}

	private void saveState() {
		final Coleccion coleccion = new Coleccion();
		coleccion.setTitulo(this.mTitleText.getText().toString());
		coleccion.setDescripcion(this.mBodyText.getText().toString());

		if ((coleccion.getTitulo() != null && !"".equals(coleccion.getTitulo()))
				|| (coleccion.getDescripcion() != null && !"".equals(coleccion.getDescripcion()))) {
			if (this.mRowId == null) {
				final long id = this.mDbHelper.crearColeccion(coleccion);
				if (id > 0) {
					this.mRowId = id;
				}
			} else {
				this.mDbHelper.modificarColeccion(this.mRowId, coleccion);
			}
		}
	}

	@Override
	protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		this.confirmButton.clearColorFilter();
	}

	public void showConfirmDialog() {
		// Create an instance of the dialog fragment and show it
		final DialogFragment dialog = new ConfirmacionEliminarDialogFragment();
		dialog.show(this.getSupportFragmentManager(), "NoticeDialogFragment");
	}

	protected void eliminarColeccion() {
		this.mDbHelper.eliminarColeccion(this.mRowId);
	}

	@Override
	public void onDialogPositiveClick(final DialogFragment dialog) {
		this.eliminarColeccion();
		ColeccionEdit.this.setResult(RESULT_OK);
		ColeccionEdit.this.finish();

	}

	@Override
	public void onDialogNegativeClick(final DialogFragment dialog) {
		dialog.dismiss();

	}
}
